/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rafkind.crawl.generator;

import com.rafkind.crawl.generator.monsters.Area;
import com.rafkind.crawl.generator.monsters.MonsterTypes;
import com.rafkind.crawl.generator.monsters.MonsterSuperType;
import com.rafkind.crawl.generator.monsters.MonsterType;
import com.rafkind.crawl.generator.monsters.SubArea;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 *
 * @author drafkind
 */
public class Generator {
  public static void main(String[] args) {
    try {
      Map<String, Class> schema = new HashMap<String, Class>();
      schema.put("monsterTypes", MonsterTypes.class);
      schema.put("superType", MonsterSuperType.class);
      schema.put("type", MonsterType.class);

      MonsterTypes m = Unmarshaller.build(
              MonsterTypes.class.getResourceAsStream("monsterTypes.xml"),
              MonsterTypes.class,
              schema);

      List<MonsterType> monsters = new ArrayList<MonsterType>();
      for (MonsterSuperType mst : m.getSuperTypes()) {
        for (MonsterType mt : mst.getTypes()) {
          monsters.add(mt);
        }
      }

      // sort by level
      Collections.sort(monsters, new Comparator<MonsterType>() {
        public int compare(MonsterType o1, MonsterType o2) {
          int c = o1.getColor().compareTo(o2.getColor());
          if (c == 0)
            c = o1.getLevel() - o2.getLevel();
          if (c == 0)
            c = o1.getName().hashCode() - o2.getName().hashCode();
          return c;
        }
      });

      Map<String, Integer> counts = new HashMap<String, Integer>();
      Map<String, List<MonsterType>> byColor = new HashMap<String, List<MonsterType>>();

      // split up into colors
      for (MonsterType mo: monsters) {
        Integer count = counts.get(mo.getColor());
        if (count == null) {
          count = 1;
        } else {
          count += 1;
        }
        counts.put(mo.getColor(), count);

        List<MonsterType> c = byColor.get(mo.getColor());
        if (c == null) {
          c = new ArrayList<MonsterType>();
          byColor.put(mo.getColor(), c);
        }
        c.add(mo);
      }

      String[][] colorChoices = new String[][] {
        {"G", "W", "R"},
        {"R", "G", "B"},
        {"U", "W", "B"},
        {"W", "U", "G"},
        {"B", "R", "U"},
        {"B", "U", "R"},
        {"W", "G", "U"},
        {"U", "B", "W"},
        {"R", "B", "G"},
        {"G", "R", "W"}
      };

      List<Area> levels = new ArrayList<Area>();
      for (String[] colorChoice : colorChoices) {
        List<MonsterType> main = byColor.get(colorChoice[0]);
        List<MonsterType> side1 = byColor.get(colorChoice[1]);
        List<MonsterType> side2 = byColor.get(colorChoice[2]);

        Area area = new Area();

        // add 7 of base
        SubArea easy = new SubArea();
        for (int i = 0; i < 7; i++)
          easy.add(main.remove(0));
        area.add(easy);

        // med = 4 base + 3 side
        SubArea med = new SubArea();
        for (int i = 0; i < 4; i++)
          med.add(main.remove(0));
        for (int i = 0; i < 3; i++)
          med.add(side1.remove(0));
        area.add(med);

        // hard - 4 base + 3 side
        SubArea hard = new SubArea();
        for (int i = 0; i < 4; i++)
          hard.add(main.remove(0));
        for (int i = 0; i < 3; i++)
          hard.add(side2.remove(0));
        area.add(hard);

        levels.add(area);
      }


      Random random = new Random();
      
      int a = 1;
      int targetAttack = 60;
      int targetDefence = 20;
      int count = 0;
      for (Area area : levels) {
        System.out.println("Area " + (a++));
        int s = 1;
        for (SubArea sub : area.getSubAreas()) {
          System.out.println("  Subarea " + (s++));

          targetAttack += ((count / 4)+1) * 10;
          targetDefence += ((count / 4)+1) * 30;

          sub.generateGroups(random, targetAttack, targetDefence, (count+1) * 20, getExpForLevel(count) / 10);

          sub.print();
          count += 2;
          /*for (MonsterType n : sub.getMonsters()) {
            System.out.println("    " + n.getColor() + n.getLevel() + " " + n.getName());
          }*/


          /*for (Group gr : g) {
            gr.print();
          }*/
        }
      }
      
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private static int getExpForLevel(int level) {
    if (level <= 1)
      return 30;
    if (level >= 55)
      return 1000000;
    else
      return (int) Math.round(getExpForLevel(level-1) * 1.2 + 5);
  }
}